const App = {
    data() {
        return {
            value: [20],
            selectedValue: '',
            IDNumber: '',
            dropdownVisible: false,
            input1: '',
            options: [
                { value: '1', label: '选项1' },
                { value: '2', label: '选项2' },
                { value: '3', label: '选项2' },
                { value: '4', label: '选项2' },
                { value: '5', label: '选项2' },
                { value: '6', label: '选项2' },
                { value: '7', label: '选项2' },
                { value: '8', label: '选项2' },
                { value: '9', label: '选项2' },
                { value: '10', label: '选项2' },
                { value: '11', label: '选项2' },
                { value: '12', label: '选项2' },
                { value: '13', label: '选项2' },
                { value: '14', label: '选项2' },
                // 更多选项...
            ],
            tableData: [
                { name: '张三', age: 20, address: '北京市' },
                { name: '李四', age: 25, address: '上海市' },
                { name: '王五', age: 30, address: '广州市' },

            ],
            tudi: [],
            government_agencies: [],
            is: true,
            websocket: null,
            num1: 0,
            arr: [],
            res: {},
            is_aciton: 'Add',
            arr: []
        }
    },
    mounted() {
        console.log(this.arr);
        function config(arr) {
            return arr.map(item => {
                let obj = {
                    id:item.Model_Number,
                    title:item.Model_Name,
                    icon:item.Icon,
                    type: item.Url ? 1:0,
                    href: item.Url
                };
                if (item.Url) {
                    obj.openType = '_iframe'
                } else if(item.children && item.children.length > 0) {
                    obj.children = config(item.children);
                }
                return obj
            })
        }
        let arr = this.arr.map(item => {
            let obj = {
                id:item.Model_Number,
                title:item.Model_Name,
                icon:item.Icon,
                type: item.Url ? 1:0,
                href: item.Url
            };
            if (item.Url) {
                obj.openType = '_iframe'
            } else if(item.children && item.children.length > 0) {
                obj.children = config(item.children)
            }
            return obj
        })
        console.log(JSON.stringify(arr));
        // $.ajax({
        //     url: `./js/1e10495a3dee96f7981df8ec46e6aa41_32be172e07132d2529afc19fb01ef78d_8.json`,
        //     method: "GET",
        //     success: (data) => {
        //         console.log(data);
        //         this.tudi = data.features.map(i => {
        //             return { data: i.properties, coords: JSON.stringify( i.geometry.coordinates) }
        //         })
        //         // let num = 0;
        //         // console.log(this.tudi[this.num].coords);
        //         // console.log(this.tudi[this.num].data.BSM);
        //     }
        // })
        // $.ajax({
        //     headers: requestHeaders(),
        //     url: `${mh}/generalBusiness/generalQueryByTableName`,
        //     type: "POST",
        //     data: {
        //         Data: 'government_agencies',
        //         Condition: "Row_Status='0'",
        //     },
        //     success: (data) => {
        //         this.government_agencies = data; // 直接使用 this

        //     },
        //     complete: (XMLHttpRequest) => {
        //         requestPretreatment(XMLHttpRequest);
        //     },
        //     dataType: "json",
        // });
        // this.websocket = new WebSocketClient();
        // this.websocket.init({
        //     url: `${ws}/generalSave/${getCookie('token')}`,
        //     onopen: () => {
        //         console.log('websocket已开启');
        //         // this.submitSave(arr);
        //     },
        //     onmessage: (data) => {
        //         try {
        //             const res = JSON.parse(data);
        //             if (res.REDIRECT && res.REDIRECT == 'REDIRECT') {//重定向
        //                 removeCookie("token");
        //                 removeCookie("user");
        //                 sessionStorage.setItem("REDIRECT", "登录已过期，请重新登录！");
        //                 window.parent.document.location.href = absolutepath() + res.CONTENTPATH;
        //             } else {
        //                 if (this.is) {
        //                     this.submitSave(this.arr, this.num1);
        //                 } else {
        //                     this.websocket.close();
        //                 }
        //             }
        //         } catch (error) { }
        //     },
        // })
        // window.addEventListener("beforeunload", ()=>{
        //     console.log(1);
        // });
    },
    methods: {
        text(Arg_str) {
            console.log(Arg_str);
            let ls_action = 'Add';
        },
        han(val) {

            console.log(this.tudi[parseInt(val)].coords);
            console.log(this.tudi[parseInt(val)].data.BSM);
        },
        handle() {
            this.arr = [];
            this.tudi.forEach(({ data, coords }, index) => {
                this.arr.push({
                    Natural_Resource_SerialNumber: data.BSM,
                    Natural_Resource_Name: data.TBXHMC,
                    Area: data.SHAPE_Area,
                    Resource_Category_SerialNumber: '22',
                    Natural_Resource_Usage_SerialNumber: data.DLBM,
                    Natural_Resource_Ownership_Rights: data.QSXZ,
                    Coords: coords,
                    QSDWDM: data.QSDWDM,
                    DLMC: data.DLMC,
                    QSDWMC: data.QSDWMC,
                    ZLDWMC: data.ZLDWMC,
                    TBXHMC: data.TBXHMC,
                    ZZSXMC: data.ZZSXMC,
                    TBMJ: data.TBMJ,
                    YSDM: data.YSDM,
                    DKBM: data.DKBM,
                    DKMC: data.DKMC,
                    SYQXZ: data.SYQXZ,
                    DKLB: data.DKLB,
                    TDLYLX: data.TDLYLX,
                    DLDJ: data.DLDJ,
                    TDYT: data.TDYT,
                    SCMJ: data.SCMJ,
                    ZJRXM: data.ZJRXM,
                    ZLDWDM: data.ZLDWDM,
                    Government_Agencies_SerialNumber: this.government_agencies.filter(i => i.Agensies_Name == data.QSDWMC)[0] ? this.government_agencies.filter(i => i.Agensies_Name == data.QSDWMC)[0].Government_Agencies_SerialNumber : '',
                    SFJBNT: data.ZZSXDM == 'LS' ? '1' : '0',
                    Province_Number: '230000',
                    City_Number: '232700',
                    County_Nubmer: '232723',
                    Town_Number: '232723104',
                    Village_SerialNumber: '232723104201',
                })
            });
            // console.log(this.arr);
            // return 
            this.submitSave(this.arr);
        },
        handle1() {
            this.arr = this.tudi.map(i => i.data);
            this.submitSave1(this.arr);
        },
        submitSave1(arr, num = 0) {
            this.num1 = num;
            let natural_resources_management = [];
            for (let i = 0; i < 50; i++) {
                if (this.num1 < arr.length) {
                    natural_resources_management.push(arr[this.num1]);
                    this.num1++
                } else {
                    this.is = false
                    break;
                }
            }
            let str = natural_resources_management.map(i => "'" + i.BSM + "'").join(",");
            //1.查询BSM
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'natural_resources_management',
                    Condition: `Natural_Resource_SerialNumber IN (${str})`,
                },
                success: (data) => {
                    data.forEach(item => {
                        let obj = natural_resources_management.filter(i => i.BSM == item.Natural_Resource_SerialNumber)[0];
                        item.DLMC = obj.DLMC;
                        item.QSDWMC = obj.QSDWMC;
                        item.ZLDWMC = obj.ZLDWMC;
                        item.TBXHMC = obj.TBXHMC;
                        item.ZZSXMC = obj.ZZSXMC;
                        item.TBMJ = obj.TBMJ;
                        item.YSMD = obj.YSMD;
                        item.DKBM = obj.DKBM;
                        item.DKMC = obj.DKMC;
                        item.STQXZ = obj.STQXZ;
                        item.DKLB = obj.DKLB;
                        item.TDLYLX = obj.TDLYLX;
                        item.DLDJ = obj.DLDJ;
                        item.TDYT = obj.TDYT;
                        item.SCMJ = obj.SCMJ;
                        item.ZJRXM = obj.ZJRXM;
                        delete item.Coords;
                        delete item.Coords_Original;
                    })
                    let lJSONObject_DataSet = {
                        Modify: {
                            natural_resources_management: data,
                        },
                        Add: {}
                    }
                    // let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                    // lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    //     Operation_Type: '0',
                    //     Operation_Path: 'view/fund_management/land_registration/add.html?action=Add',
                    //     Operation_Main,
                    //     Operation_Interface: "generalSaveByRequestBody",
                    //     Operation_IP: getIpAddress()
                    // }]
                    this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, AccessToken: getCookie('token') }));
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
            //2.修改
            //3.提交

        },
        submitSave(arr, num = 0) {
            let natural_resources_bsm_usage = [];
            this.num1 = num;

            for (let i = 0; i < 50; i++) {
                if (this.num1 < arr.length) {
                    natural_resources_bsm_usage.push(arr[this.num1]);
                    this.num1++
                } else {
                    this.is = false
                    break;
                }
            }


            let lJSONObject_DataSet = {
                Add: {
                    natural_resources_bsm_usage,
                }
            }

            // let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
            // lJSONObject_DataSet['Add']['base_operation_log'] = [{
            //     Operation_Type: '0',
            //     Operation_Path: 'view/fund_management/land_registration/add.html?action=Add',
            //     Operation_Main,
            //     Operation_Interface: "generalSaveByRequestBody",
            //     Operation_IP: getIpAddress()
            // }]
            // console.log(JSON.stringify(lJSONObject_DataSet) );
            this.websocket.send(JSON.stringify({ Data: lJSONObject_DataSet, AccessToken: getCookie('token') }));
        },
        handleRowClick(row) {
            console.log(row);
        },
        handleVisibleChange(visible) {
            console.log(visible);
            this.dropdownVisible = visible;
        },
        test(num) {
            var valnum;
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            var nTemp = 0, i;
            for (i = 0; i < 17; i++) {
                nTemp += num.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[nTemp % 11];
            return valnum
        },
        check() {
            $.ajax({
                headers: {
                    "content-type": "application/x-www-form-urlencoded",
                    "Session": 'session5'
                },
                url: `htps:/service.essp.top/apiForMoHe/officeOperation/convertWordToPdf`,
                method: "post",
                data: {
                    TableName: 'agricultural_machinery_operation',
                    Condition: "",
                    Original: '1'
                },
                success: (res) => {

                }
            })
        },
        success(res, raw) {
            console.log(raw);
            // this.uploadBinaryFile(raw);
        },
        remove() {

        },
        clear() {

        },
        handleUpload({ raw }) {

        },
        async uploadBinaryFile({ raw }) {
            try {
                const response = await fetch(
                    // `${mh}/officeOperation/convertWordToPdf`,
                    `${'http://3t451716y8.qicp.vip'}/officeOperation/convertPDFToImage`,
                    // `${mh}/officeOperation/convertPDFToImage`,
                    {
                        method: "POST",
                        headers: {
                            "Content-Type": "application/octet-stream",
                            "X-File-Name": encodeURIComponent(raw.name),
                            "X-File-Size": raw.size,
                            "X-File-Type": raw.type,
                            "AccessToken": getCookie("token"),
                        },
                        body: raw,
                    }
                );
                console.log(await response);
                const result = await response.json();
                console.log();
                console.log(result);
                return result;
            } catch (error) {
                throw error;
            }
        }
    },
};


const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-select', ylSelect);
app.component('yl-upload', ylUpload);
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus);
app.mount("#app");
